package com.smartleanx.module.hrm.controller.admin.examine.examineProcess;

import cn.hutool.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineProcess.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineProcess.ExamineProcessDO;
import com.smartleanx.module.hrm.service.examine.examineProcess.ExamineProcessService;

@Tag(name = "管理后台 - 记录")
@RestController
@RequestMapping("/hrm/examineProcess")
@Validated
public class ExamineProcessController {

    @Resource
    private ExamineProcessService examineProcessService;

    @PostMapping("/create")
    @Operation(summary = "创建记录")
    @PreAuthorize("@ss.hasPermission('hrm:examine-process:create')")
    public CommonResult<Integer> createExamineProcess(@Valid @RequestBody ExamineProcessSaveReqVO createReqVO) {
        return success(examineProcessService.createExamineProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新记录")
    @PreAuthorize("@ss.hasPermission('hrm:examine-process:update')")
    public CommonResult<Boolean> updateExamineProcess(@Valid @RequestBody ExamineProcessSaveReqVO updateReqVO) {
        examineProcessService.updateExamineProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-process:delete')")
    public CommonResult<Boolean> deleteExamineProcess(@RequestParam("id") Integer id) {
        examineProcessService.deleteExamineProcess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-process:query')")
    public CommonResult<ExamineProcessRespVO> getExamineProcess(@RequestParam("id") Integer id) {
        ExamineProcessDO examineProcess = examineProcessService.getExamineProcess(id);
        return success(BeanUtils.toBean(examineProcess, ExamineProcessRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "列表")
    public CommonResult<List<JSONObject>> selectList(Integer employeeId, Integer examineId) {
        List<JSONObject> result = examineProcessService.selectList(employeeId,examineId);
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出记录 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-process:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineProcessExcel(@Valid ExamineProcessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineProcessDO> list = examineProcessService.getExamineProcessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "记录.xlsx", "数据", ExamineProcessRespVO.class,
                        BeanUtils.toBean(list, ExamineProcessRespVO.class));
    }

}